import {Body, Controller, Delete, Get, Param, Patch, Post, Req, UseGuards,} from "@nestjs/common";
import {JwtAuthGuard} from "../auth/guards/jwt-auth.guard";
import {ModuleService} from "../module/module.service";
import {RespWrap} from "src/common/base.utils";
import {UpdateModuleDto} from "./dto/update.module.dto";
import {CreateModuleDto} from "./dto/create.module.dto";
import {ModuleEntity} from "./module.entity";
import {getRepository} from 'typeorm'

@UseGuards(JwtAuthGuard)
@Controller("modules")
export class ModuleController {
  constructor(private moduleService: ModuleService) {
  }


  @Get(':id')
  async get(@Param() params) {
    return RespWrap(await getRepository(ModuleEntity).findOne({id: params.id}));
  }

  @Get()
  async pageQuery(@Req() req) {
    const resp = await this.moduleService.pageQuery({
      ...req.query,
    });
    return RespWrap(resp);
  }

  @Post()
  async insert(@Body() body: CreateModuleDto, @Req() req) {
    body.userId = req.user.id;
    const resp = await this.moduleService.insert(body);
    return RespWrap(resp);
  }

  @Delete(":id")
  async removeById(@Param() params) {
    const resp = await this.moduleService.removeById(params.id);
    return RespWrap(resp);
  }

  @Patch(":id")
  async update(@Param() params, @Body() body: UpdateModuleDto) {
    const resp = await this.moduleService.update({
      id: params.id,
      ...body,
    });
    return RespWrap(resp);
  }
}
